<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Episode
 *
 * @property int $id
 * @property string $raw_name
 * @property string $provider
 * @property string $url_provider
 * @property int $season
 * @property int $episode
 * @property int $show_id
 * @property string $size
 * @property string $quality
 * @property bool $is_auth
 * @property string $url_tvmaze
 * @property string $url_douban
 * @property string $url_torrent
 * @property string $url_magnet
 * @property string $url_ed2k
 * @property string $url_baidupan
 * @property string $url_other
 * @property string $url_video
 * @property int $like_count
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property int $movie_id
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Article[] $articles
 * @property-read mixed $body
 * @property-read mixed $bread_crumb
 * @property-read mixed $coverage_image
 * @property-read mixed $coverage_url
 * @property-read mixed $full_name
 * @property-read mixed $image_url
 * @property-read mixed $key_word
 * @property-read mixed $name_en
 * @property-read mixed $name_zh
 * @property-read mixed $search_content
 * @property-read mixed $search_tag
 * @property-read mixed $season_episode
 * @property-read mixed $show_list
 * @property-read mixed $show_url
 * @property-read mixed $site_url
 * @property-read mixed $thumbnail_url
 * @property-read \App\Movie $movie
 * @property-read \App\Show $show
 * @method static \Illuminate\Database\Query\Builder|\App\Episode baiduPan()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode beforeYesterday()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode britain()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode cooked()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode documentary()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode episodeMovied()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode episodeUnmovied()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode japan()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode korean()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode movieEp()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode talkShow()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode thailand()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode today()
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereEpisode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereIsAuth($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereLikeCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereMovieId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereProvider($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereQuality($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereRawName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereSeason($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereShowId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereSize($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlBaidupan($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlDouban($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlEd2k($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlMagnet($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlOther($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlProvider($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlTorrent($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlTvmaze($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereUrlVideo($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Episode yesterday()
 * @mixin \Eloquent
 * @property string $name 重新计算的结果
 * @method static \Illuminate\Database\Query\Builder|\App\Episode whereName($value)
 */
class Episode extends Model
{
    /**
     * 要触发的所有关联关系
     *
     * @var array
     */
    //protected $touches = ['show'];


    protected $fillable = [
        'raw_name',
        'provider',
        'season',
        'episode',
        'show_id',
        'size',
        'is_auth',
        'quality',
        'url_tvmaze',
        'url_magnet',
        'url_douban',

        'url_ed2k',
        'url_torrent',
        'url_baidu',
        'url_other',
        'url_video',
        'url_provider',
        ];
    public function movie() {
        return $this->belongsTo('App\Movie');
    }
    public function articles() {
        return $this->belongsToMany('App\Article');
    }
    public function show() {
        return $this->belongsTo('App\Show');
    }
    public function setRawNameAttribute($value) {
        $this->attributes['raw_name'] = $value;
        //提取 季数
        if (preg_match('#s\d\d#i',$value, $matches)) {
            $seasonNumber = $matches[0];
            $seasonNumber = trim(str_ireplace('s','',$seasonNumber));
            $this->attributes['season'] = $seasonNumber;
        }
        //提取 集数

        if (preg_match('#e\d\d#i',$value, $matches)) {
            $episodeNumber = $matches[0];
            $episodeNumber = trim(str_ireplace('e','',$episodeNumber));
            $this->attributes['episode'] = $episodeNumber;
        }
    }
    public function getCoverageUrlAttribute() {
        $size=200;
        $images = $this->show->images;
        if($images->count()== 0) {
            $key = '1461424563';
            $waterMaker = 'imageView2/1/w/'.$size.'/h/'.$size;
            $baseUrl = 'http://img.cnfluffy.com';
            return $baseUrl.'/'.$key.'?'.$waterMaker;
        }else {
            return  $images->first()->squareImage($size);
        }
    }
    public function getNameEnAttribute() {
        if($this->show) {
            return $this->show->name_en;
        }
        if($this->movie) {
            return $this->movie->name_en;
        }
    }
    public function getNameZhAttribute() {
        if($this->show) {
            return $this->show->name_zh;
        }
        if($this->movie) {
            return $this->movie->name_zh;
        }
    }

    public function getSiteUrlAttribute () {
        return env('APP_URL').'/episode/'.$this->id;
    }
    public function getShowUrlAttribute () {
        return url('show',$this->show);
    }
    public function getFullNameAttribute () {
        $eztv_name = str_ireplace('[eztv]','',$this->raw_name);
        return $eztv_name;
    }
    public function getKeyWordAttribute() {
        if($this->show) {
            return $this->show->name_en.'  '.$this->show->name_zh.'  '.$this->raw_name.'   百度网盘下载 迅雷下载 种子 网盘下载 磁力下载';
        }
        if($this->movie) {
            return $this->movie->name_en.'  '.$this->movie->name_zh.'  '.$this->raw_name.'   百度网盘下载 迅雷下载 种子 网盘下载 磁力下载';
        }
    }

    public function getShowListAttribute() {
        return  $this->show->lists('id')->toArray();
    }

    public function scopeEpisodeUnmovied($query) {
        $query->where('movie_id','=',0)
            ->Where('provider','like','%movie%')
            ->orderBy('raw_name');
    }
    public function scopeEpisodeMovied($query) {
        $query->where('movie_id','>',0)
                 ->Where('provider','like','%movie%')
                 ->orderBy('raw_name');
    }

    public function getCoverageImageAttribute() {
        $movie = $this->movie;
        if($movie) {
            return $movie->coverage_image;
        }
        $show = $this->show;
        if($show) {
            return $show->coverage_image;
        }
    }


    public function getBodyAttribute() {
        $movie = $this->movie;
        if($movie) {
            return $movie->detail;
        }
        $show = $this->show;
        if($show) {
            return $show->detail;
        }
    }

    public function getSeasonAttribute() {
        return sprintf('%02d',$this->attributes['season']);

    }
    public function getEpisodeAttribute() {
        return sprintf('%02d',$this->attributes['episode']);
    }
    public function getSeasonEpisodeAttribute() {

        if ($this->attributes['season'] > 0) {
            $season = sprintf('%02d',$this->attributes['season']);
            $episode = sprintf('%02d',$this->attributes['episode']);
            return 'S'.$season.'E'.$episode;
        } else  {
            return '';
        }
    }

    public function getSearchContentAttribute() {
        $content = $this->full_name;
        if($this->url_magnet) $content.='  磁力链接';
        if($this->url_ed2k) $content.='  磁力链接';
        if($this->url_baidupan) $content.='  百度网盘';
        if($this->url_other) $content.='  其他网盘';
        if($this->url_torrent) $content.='  种子下载';
        return $content;

    }
    public function getSearchTagAttribute() {

    }
    public function getBreadCrumbAttribute() {

    }
    public function getThumbnailUrlAttribute() {

    }
    public function getImageUrlAttribute() {

    }

    public function scopeBaiduPan($query) {
        $query->where('updated_at','>',Carbon::now()->subHours(48))
            ->where('url_baidupan','like','%baidu%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    public function scopeCooked($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(3))
            ->where('raw_name','like','%字幕%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    //BBC
    public function scopeDocumentary($query) {
        $query
            ->where('updated_at','>',Carbon::now()->subDays(30))
            ->where('raw_name','REGEXP','bbc|pbs|nhk|itv|nasa|series|tv5|pdtv|hbo|cbs')
                        ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    //脱口秀
    public function scopeTalkShow($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(7))
            ->where('raw_name','like','%show%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }

    public function scopeMovieEp($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(28))
            ->where('provider','like','%movie%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    public function scopeBritain($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(7))
            ->where('provider','like','%英国%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    public function scopeJapan($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(30))
            ->where('provider','like','%日本%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    public function scopeThailand($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(90))
            ->where('provider','like','%泰国%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    public function scopeKorean($query) {
        $query->where('updated_at','>',Carbon::now()->subDays(90))
            ->where('provider','like','%韩国%')
            ->orderBy('updated_at','desc')
            ->with('show','movie');
    }
    public function scopeToday($query) {
        $query->whereDate('updated_at','=',Carbon::today()->toDateString())
              ->orderBy('updated_at','desc')
              ->with('show','movie');
    }
    public function scopeYesterday($query) {
        $query->whereDate('updated_at','=',Carbon::yesterday()->toDateString())
              ->orderBy('updated_at','desc')
              ->with('show','movie');
    }
    public function scopeBeforeYesterday($query) {
        $query->whereDate('updated_at','=',Carbon::today()->subDays(2)->toDateString())
              ->orderBy('updated_at','desc')
              ->with('show','movie');
    }
}
